package cn.juforg.airled;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.SurfaceHolder;

public class AirLED extends AirLEDSurface{
	
	static Canvas toScaleCanvas;
	static Canvas scaledBitmapCanvas;
	
	static Bitmap bitmap2Scale;
	static Bitmap scaledBitmap;
	
	int bitmapStartPosition = 0;
	String displayText = "";
	String ledText = "";
	int minStep = 0;
	int numDotsPerMove = 0;
	int numHorizontalLeds;
	int numVerticalLeds;
	
	Paint textPaint;
	Paint.FontMetricsInt metrics;
	
	private int dotSize = 10;
	private int textStep = 0;
	private boolean initialized;
	private Typeface typeface = Typeface.MONOSPACE;
	
	
	int color;
	int speed;
	String font;
	
	public AirLED(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public AirLED(Context context) {
		super(context);
	}

	private void drawText(Canvas paramCanvas){
		if(this.bitmapStartPosition <= -getWidth()){
			this.bitmapStartPosition = 0;
			this.displayText = parseText(ledText);
			if (this.textStep <= this.minStep)
				this.textStep = (2 * this.numHorizontalLeds);
			this.textStep -= this.numHorizontalLeds;
			toScaleCanvas.drawColor(Color.BLUE);
			toScaleCanvas.drawText(this.displayText, this.textStep, this.numVerticalLeds, this.textPaint);
			scaledBitmapCanvas.drawBitmap(bitmap2Scale, new Rect(0, 0, bitmap2Scale.getWidth(), bitmap2Scale.getHeight()),
														new Rect(0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight()) ,null);
		}
		
		this.bitmapStartPosition -= this.numDotsPerMove * this.dotSize;
		paramCanvas.drawBitmap(scaledBitmap, this.bitmapStartPosition, 0.0F, null);
	}
	
	private String parseText(String paramString){
		this.displayText = paramString;
		this.displayText = this.displayText.replaceAll("%d", android.text.format.DateFormat.getMediumDateFormat(getContext()).format(Long.valueOf(System.currentTimeMillis())));
		this.displayText = this.displayText.replaceAll("%t", android.text.format.DateFormat.getTimeFormat(getContext()).format(Long.valueOf(System.currentTimeMillis())));
		return this.displayText;
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder surfaceholder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void doDraw(Canvas paramCanvas) {
		if (this.initialized) {
			drawText(paramCanvas);
		} else {
			initialize();
		}
	}
	
	public void initialize(){
		this.displayText = parseText(this.ledText);
	    if (bitmap2Scale != null)
	      bitmap2Scale.recycle();
	    bitmap2Scale = null;
	    if (scaledBitmap != null)
	      scaledBitmap.recycle();
	    scaledBitmap = null;
	    System.gc();
	    this.numHorizontalLeds = (getWidth() / this.dotSize);
	    this.numVerticalLeds = (getHeight() / this.dotSize);
	    setupText();
	    if (this.numDotsPerMove > 0)
	    {
	      this.textStep = this.numHorizontalLeds;
	      try
	      {
	        while (scaledBitmap == null)
	          scaledBitmap = Bitmap.createBitmap(2 * (this.numHorizontalLeds * this.dotSize), this.numVerticalLeds * this.dotSize, Bitmap.Config.RGB_565);
	      }
	      catch (Error localError)
	      {
	      }
	    }
	    while (true)
	    {
	      toScaleCanvas = new Canvas(bitmap2Scale);
	      toScaleCanvas.drawColor(Color.BLACK);
	      toScaleCanvas.drawText(this.displayText, this.textStep, this.numVerticalLeds - this.metrics.bottom, this.textPaint);
	      scaledBitmapCanvas = new Canvas(scaledBitmap);
	      scaledBitmapCanvas.drawColor(Color.BLACK);
	      scaledBitmapCanvas.drawBitmap(bitmap2Scale, new Rect(0, 0, bitmap2Scale.getWidth(), bitmap2Scale.getHeight()), new Rect(0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight()), null);
	      this.minStep = (-(int)(this.textPaint.measureText(this.displayText) + this.numHorizontalLeds));
	      this.bitmapStartPosition = 0;
	      this.initialized = true;
	      this.textStep = 0;
	      while (bitmap2Scale == null)
	        bitmap2Scale = Bitmap.createBitmap(2 * this.numHorizontalLeds, this.numVerticalLeds, Bitmap.Config.RGB_565);
	      break;
	    }
	    return;
	}
	
	private void setupText(){
		float f1 = 1.0F;
	    this.textPaint = new Paint();
	    this.textPaint.setAntiAlias(false);
	    this.textPaint.setFilterBitmap(false);
	    this.textPaint.setDither(true);
	    this.textPaint.setColor(this.color);
	    this.textPaint.setAlpha(255);
	    this.textPaint.setTypeface(this.typeface);
	    this.textPaint.setTextAlign(Paint.Align.LEFT);
	    this.textPaint.setFakeBoldText(true);
	    this.textPaint.setTextSize(f1);
	    this.metrics = this.textPaint.getFontMetricsInt();
	    while (true)
	    {
	      if (-this.metrics.top + this.metrics.bottom >= 4 * (this.numVerticalLeds / 5))
	        return;
	      Paint localPaint = this.textPaint;
	      float f2 = f1 + 1.0F;
	      localPaint.setTextSize(f1);
	      this.metrics = this.textPaint.getFontMetricsInt();
	      f1 = f2;
	    }
	}
	public void reinitialize(){
		this.initialized = false;
	}

	public void setLedText(String ledText) {
		this.ledText = ledText;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public void setFont(String font) {
		this.font = font;
	}

	
	
}
